<?php

/**
 * This is the model class for table "tracking".
 *
 * The followings are the available columns in table 'tracking':
 * @property string $id_tracking
 * @property integer $id_client
 * @property integer $id_tpdetail
 * @property string $ship_date
 * @property integer $status
 * @property string $date_add
 * @property string $date_upd
 * @property integer $del_flg
 */
class Tracking extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Tracking the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tracking';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_client, id_tpdetail, ship_date', 'required'),
			array('id_client, id_tpdetail, status, del_flg', 'numerical', 'integerOnly'=>true),
			array('date_add, date_upd', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_tracking, id_client, id_tpdetail, ship_date, status, date_add, date_upd, del_flg', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'track_task' => array(self::HAS_MANY, 'TrackingTask', 'id_tracking'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_tracking' => '#',
			'id_client' => Yii::t('app', 'Client'),
			'id_tpdetail' => Yii::t('app', 'Transporter'),
			'ship_date' => Yii::t('app', 'Shipping date'),
			'status' => Yii::t('app', 'Status'),
			'date_add' => 'Date Add',
			'date_upd' => 'Date Upd',
			'del_flg' => 'Del Flg',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

//        $criteria->with = array('track_task');
        $criteria->addCondition('t.del_flg = 0');
//        $criteria->addCondition('del_flg = 0');

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    public function beforeSave() {
        $now = new CDbExpression('NOW()');
        if ($this->isNewRecord) $this->date_add = $now;

        $this->date_upd = $now;

        return parent::beforeSave();
    }
}